package cc.linker.steplink.crm.service.impl;

import cc.linker.steplink.crm.dao.ArticleRuleDao;
import cc.linker.steplink.crm.domain.ArticleRule;
import cc.linker.steplink.crm.response.GlobalException;
import cc.linker.steplink.crm.response.ResponseException;
import cc.linker.steplink.crm.service.IArticleRuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class ArticleRuleImpl implements IArticleRuleService {

    @Autowired
    private ArticleRuleDao articleRuleDao;

    @Override
    public ArticleRule getArticleRule(String orgId) throws Exception {
        if (StringUtils.isEmpty(orgId)) {
            throw new GlobalException(new ResponseException(400, "机构id为空"));
        }
        if(articleRuleDao.ifExist(orgId)<1){
            ArticleRule records = new ArticleRule();
            records.setOrgId(orgId);
            articleRuleDao.addArticleRule(records);
        }
        ArticleRule record = articleRuleDao.getArticleRule(orgId);
        return record;
    }

    @Override
    public Boolean addArticleRule(ArticleRule record) throws Exception {
        if (StringUtils.isEmpty(record.getOrgId())) {
            throw new GlobalException(new ResponseException(400, "机构id为空"));
        }
        int id = articleRuleDao.addArticleRule(record);
        if(id>0){
            return true;
        }
        throw new GlobalException(new ResponseException(400, "编辑规则失败"));
    }
}
